package com.jian.mall.produce.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jian.mall.produce.entity.ProductAttrValueEntity;
import com.jian.mall.produce.service.ProductAttrValueService;
import com.jian.mall.produce.service.SpuInfoService;
import com.jian.mall.produce.vo.AttrRepsVo;
import com.jian.mall.produce.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jian.mall.produce.entity.AttrEntity;
import com.jian.mall.produce.service.AttrService;
import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.R;


/**
 * 商品属性
 *
 * @author tainanle
 * @email 12345678@gmail.com
 * @date 2022-08-10 22:48:11
 */
@RestController
@RequestMapping("produce/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * http://localhost:10010/produce/attr/update/5
     * 请求方法: POST
     */
    @PostMapping("/update/{spuId}")
    public R updateBySpuId(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> values) {
        productAttrValueService.updateBySpuId(spuId, values);

        return R.ok();
    }

    // http://localhost:10010/produce/attr/base/listforspu/5
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpuBaseAttr(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntity = productAttrValueService.listBySpuId(spuId);

        return R.ok().put("data", productAttrValueEntity);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttr(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("produce:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("produce:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrRepsVo repsVo = attrService.getInfoById(attrId);
        return R.ok().put("attr", repsVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("produce:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveInfo(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("produce:attr:update")
    public R update(@RequestBody AttrVo attr) {
//		attrService.updateById(attr);
        attrService.updateVo(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("produce:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeInfoByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
